require 'test/unit'
require 'watir'

         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ $ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"
         
    class TC_06ManageRoles< Test::Unit::TestCase  
 
  #~ def test_01Login
  
         #~ require 'watir'

        #~ puts "User is going to login to the Crux Application"
        #~ sleep 5
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
        #~ $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
        #~ puts "Successfully logged into the Application"
        #~ sleep 10

   #~ end

############################################################################################################

def test_02AddRole
  
require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Add_Role.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Add Role Functionality"
puts "**************************"

myData.each do |record|  
  
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Roles").click
    sleep 5

  
      puts "Creating Role: '#{record['RoleName']}'"
 
    
    $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewRole").click
    sleep 5 
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtRoleName_txtWaterMarkedTextBox').set(record['RoleName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtRoleDescription_txtWaterMarkedTextBox').set(record['Description'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtComments_txtWaterMarkedTextBox').set(record['Comments'])
    
    prev="#{record['Privilages']}" 
    prev1=prev.split("/")
    prev2=prev1.size
    for j in 0..prev2-1
    
    $ie.label(:text,prev1[j]).click 
    
  end
  
  
    comp="#{record['CompanyTypes']}" 
    comp1=comp.split("/")
    comp2=comp1.size
    for k in 0..comp2-1
    
    $ie.label(:text,comp1[k]).click 
    
  end 
   
    
     $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
     sleep 5  
     puts "Role: '#{record['RoleName']}'created successfully"
     puts "Verifying created Role: '#{record['RoleName']}'in Roles Table"
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles").row_count

     for i in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[i][2].text 
    #puts a
    if a=="#{record['RoleName']}"      
     puts "Role: '#{record['RoleName']}' verified successfully"   
    sleep 5 

for k in 0..comp2-1   
 
    
    if comp1[k]=="InternalCompany"  
    
    puts"Verifying created Role: '#{record['RoleName']}' in Manage User details page"
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
    
    puts "Verifying existing Company : '#{record['CompanyName']}' for User details"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User: '#{record['UserName']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_user_details.gif").click  
      sleep 5
       if $ie.contains_text(record['RoleName']+': '+record['Description'])
      puts "Role: '#{record['RoleName']}' verified successfully in Manage User Details page" 
      else
       puts "Error: could not find Role: '#{record['RoleName']}' in Manage User Details page"
       end

                  break  
              end   
          end
      break
    end
  end
  break
  end
  end
 break
 end 
 end
end

puts "**************************"
puts "End of Add Role Functionality"
puts "**************************"
end

#####################################################################################################################

def test_03EditRole
  
require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Edit_Role.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Edit Role Functionality"
puts "**************************"

myData.each do |record|  
  
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Roles").click
    sleep 5
  
     puts "Verifying created Role: '#{record['RoleName']}'in Roles Table for update"
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles").row_count

     for m in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[m][2].text 
    #puts a
    if a=="#{record['RoleName']}"      
     puts "Role: '#{record['RoleName']}' verified successfully"   
     puts "Updating Role: '#{record['RoleName']}'details"  
    sleep 5 
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[m][3].button(:src,"http://"+$ip+"/CruxWebUI/appimages/Edit.gif").click
    sleep 5
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtRoleName_txtWaterMarkedTextBox').set(record['ERoleName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtRoleDescription_txtWaterMarkedTextBox').set(record['EDescription'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtComments_txtWaterMarkedTextBox').set(record['Comments'])
    sleep 5
    
    prev="#{record['EditPrivilages']}" 
    prev1=prev.split("/")
    prev2=prev1.size
    for j in 0..prev2-1    
    $ie.label(:text,prev1[j]).click    
  end
  sleep 5
  
  
    comp="#{record['ECompanyTypes']}" 
    comp1=comp.split("/")
    comp2=comp1.size
    for k in 0..comp2-1    
    $ie.label(:text,comp1[k]).click     
  end  
  sleep 5
  
     $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
     sleep 5  
     puts "Role: '#{record['RoleName']}'updated successfully"  
     puts "Verifying updated Role: '#{record['ERoleName']}'in Roles Table"
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles").row_count

     for n in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[n][2].text 
    #puts a
    if a=="#{record['ERoleName']}"      
     puts "Updated Role: '#{record['ERoleName']}' verified successfully in Roles table"   
     sleep 5   
     
     comp="#{record['FinalCompanyTypes']}" 
    comp1=comp.split("/")
    comp2=comp1.size
    

for k in 0..comp2-1   
 
    
    if comp1[k]=="InternalCompany"  
    
    puts"Verifying created Role: '#{record['RoleName']}' in Manage User details page"
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
    
    puts "Verifying existing Company : '#{record['CompanyName']}' for User details"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User: '#{record['UserName']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_user_details.gif").click  
      sleep 5
       if $ie.contains_text(record['ERoleName']+': '+record['EDescription'])
      puts "Role: '#{record['RoleName']}' verified successfully in Manage User Details page" 
      else
       puts "Error: could not find Role: '#{record['RoleName']}' in Manage User Details page"
       end

                  break  
              end   
          end
      break
    end
  end
  break
  end
  end
 break
 end 
 end
 break
 end
 end
end

puts "**************************"
puts "End of Edit Role Functionality"
puts "**************************"
end


######################################################################################################################


def test_04DeleteRole
  
require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new($filepath + "/Crux_Data/Delete_Role.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Delete Role Functionality"
puts "**************************"

myData.each do |record|  
  
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Roles").click
    sleep 5
  
     puts "Verifying created Role: '#{record['RoleName']}' in Roles Table for Delete"
 
     b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles").row_count

     for m in 2..b do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[m][2].text 
    #puts a
    if a=="#{record['RoleName']}"      
     puts "Role: '#{record['RoleName']}' verified successfully"   
     puts "Deleting Role: '#{record['RoleName']}'"  
    sleep 5 
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[m][3].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
    sleep 2  
     hwnd = $ie.enabled_popup(5)

  if (hwnd)  #yeah! a popup
  popup = WinClicker.new
  popup.makeWindowActive(hwnd)
  popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
end
sleep 8

     puts "Role: '#{record['RoleName']}'deleted successfully"
  
     puts "Verifying Deleted Role: '#{record['ERoleName']}'in Roles Table"
 
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles").row_count

     for n in 2..c do
     a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdRoles")[n][2].text 
    #puts a
    if a=="#{record['RoleName']}"      
     flag=1
     break
   end
 end

if flag==1
  puts "Role '#{record['RoleName']}' not deleted successfully"  
  else
  puts "Deleted Role: '#{record['RoleName']}' verified successfully"
  comp="#{record['CompanyTypes']}" 
  comp1=comp.split("/")
  comp2=comp1.size

for k in 0..comp2-1    
    
    if comp1[k]=="InternalCompany"  
    
    puts"Verifying created Role: '#{record['RoleName']}' in Manage User details page"
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
    
    puts "Verifying existing Company : '#{record['CompanyName']}' for User details"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User: '#{record['UserName']}'"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_user_details.gif").click  
      sleep 5
       if $ie.contains_text(record['RoleName']+': '+record['Description'])
       puts "Role: '#{record['RoleName']}' not deleted from Manage User Details page" 
       else
       puts "Deleted Role: '#{record['RoleName']}' successfully verified in Manage User Details page"
       end

                  break  
              end   
          end
      break
    end
  end
  break
  end
  end
end
 break
 end 
 end
end

puts "**************************"
puts "End of Delete Role Functionality"
puts "**************************"
end


  def test_05Logout
    
            puts "Logging out from the Application"
            $ie.link(:id,"ctl00_Logout_lnkLogout").click
            puts "Successfully logged out from the Application"
            puts "********************************"
            puts "End of Crux Admin Functionality"
            puts "********************************"
            sleep 5
            $ie.close
            
          end


end
